<?php
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}

    $objXajax->registerFunction('fmfrmchartgeneral_viewchart'); 
   
    function fmfrmchartgeneral_viewchart($charttype=1,$dateFrom='',$dateTo=''){
    
        global $objDbSelect,$arrTitle;
        
         $arrTitle = array(
            1=>'Doanh thu theo ngày',
            2=>'Doanh thu theo khu vực',
            3=>'Theo người thu' ,
            4=>'Tiền bán sản phẩm theo ngày'
        );
        $objResponse = new xajaxResponse();
        include(ROOT_PATH.'adagps.com/Charts/ofc/php-ofc-library/open-flash-chart.php');    
        $dateFrom = $dateFrom==''?SYSTEM_DATE_START  : $dateFrom;
        $dateTo = $dateTo==''?SYSTEM_DATE  : $dateTo;
        $arrResult = fmfrmchartgeneral_createArray($charttype,$dateFrom,$dateTo);
        
        $arrName = Array(); 
        $max = 5;
        $title = new title( $arrTitle[$charttype],20) ;
        $format = 2;
        $sName='';
        $max =0;
        $min =0;
        $chart = new open_flash_chart();
        $barLabel = array();
        $barArr = array();
        $bar =0;
        
        if($charttype==1||$charttype==4){
            //echo FormatMoney('321123.123456'); die();
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['date_sum'];
                $xx = new x_axis_label($sName, '#050069', 13, 340);            
                $barLabel[] = $xx;
                $dec = round((int)$arrResult[$i]['money_total'],-3);
                if($dec>$max)$max=$dec;
                if($dec<$min)$min=$dec;
                $d = new dot($dec);               
                $barArr[] = $d->colour('#D02020')->tooltip($sName."<br>".FormatMoney($dec));  
               
              }
              $bar = new line();
              $bar->set_tooltip('Value = #val');
              $bar->set_on_show(new line_on_show('grow-up', 2.5, 0));
              $bar->set_values($barArr); 
              
             
        }else if($charttype==5){
            $barArr = array();
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['date_sum'];
                $xx = new x_axis_label($sName, '#050069', 13, 340);            
                $barLabel[] = $xx;
                $dec = (int) round((int)$arrResult[$i]['money_total'],-3);
                $barArr[] = $dec; 
                if($dec>$max)$max=$dec;
                if($dec<$min)$min=$dec;

            }
           // die();
            $bar = new bar_glass();
            $bar->set_colour('#68D5ED');
            $bar->set_values($barArr);
            $bar->key($arrTitle[$charttype],12); 
            
        }else if($charttype==3){
            $barArr = array();
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['date_sum'];
                $xx = new x_axis_label($sName, '#050069', 13, 340);            
                $barLabel[] = $xx;
                $dec = round((int)$arrResult[$i]['money_total'],-3);
                $barArr[] = $dec;
                if($dec>$max)$max=$dec;
                if($dec<$min)$min=$dec;  

            }
            $bar = new bar_glass();
            $bar->set_colour('#68D5ED');
            $bar->set_values($barArr);
        }
        else if($charttype==2){
            $barArr = array();
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['date_sum'];
                //if($arrResult[$i]['numdw']!=0){
                  $dec = round((int)$arrResult[$i]['money_total'],-3);  
               // }                
                 array_push($barArr,new pie_value($dec,addslashes("$sName")));

            }
            $bar = new pie();  
            $bar->set_alpha(0.8);
            $bar->set_start_angle( 35 );
            $bar->add_animation( new pie_fade() )->add_animation( new pie_bounce(10) ) ; 
            $bar->set_tooltip( '#val# VNĐ <br> #percent# ' );
            $bar->set_colours(array('#1C9E05','#FF368D','#0000FF','#06DCFB','#FF800D','#B96F6F','#06DCFB'));
            $bar->set_values( $barArr );

        } 
        if($charttype!=2){
            $rangeMax =0;
            $rangeMin =0;
            $rangStep=1;
            createRangeForChart($min,$max,$rangeMax,$rangeMin);
            $y = new y_axis();
            $y->set_range($rangeMin,$rangeMax);
            $y->set_steps(($rangeMax-$rangeMin)/5);
            
            $x_labels = new x_axis_labels();
            $x = new x_axis();
            $x_labels->set_labels($barLabel );
            $x->set_labels($x_labels);
               
            $chart->set_x_axis($x);
            $chart->set_y_axis($y);
            
        }
        
        

        $chart->add_element($bar);
        $chart->set_title($title);        
        $values = $chart->toPrettyString();
        
       $sScript = 'data_fmchartgeneral=' . $values .';';                
        
       $objResponse->addScript($sScript);    
        $sScript1 = 'swfobject.embedSWF("adagps.com/Charts/ofc/open-flash-chart.swf", "fmfrmchartgeneral_divGrid", "75%", "450", "9.0.0", "expressInstall.swf", {"get-data":"open_flash_chart_data_fmchartgeneral"}, {"wmode":"opaque"});';
        $objResponse->addScript($sScript1);  
        $objResponse->addScript("changeWaiting(1);");  
                          
        return $objResponse->getXML();    
    }
    
function fmfrmchartgeneral_createArray($charttype,$dateFrom,$dateTo){
    global $objDbUpdate,$tbl_postfix,$hotel_id;
   
   $sql = '';
    switch($charttype){
        case 1:  
        $sql = " select money_revenue as money_total,DATE_FORMAT(date_sum,'%d/%m/%Y') date_sum from fm_summary where service_id = $hotel_id 
        and datediff(date_sum,'$dateFrom')>=0 and datediff(date_sum,'$dateTo') <= 0 order by date_sum asc" ;
        break;
        case 2: 
        $sql = " select sum(a.num*(a.price +a.area_extprice)) as money_total, b.name as date_sum from cus_history_product$tbl_postfix a join 
        fm_room c on c.id = a.table_id and c.service_id = $hotel_id join fm_room_type b on c.room_type = b.id and b.service_id = $hotel_id
        and c.service_id = $hotel_id where datediff(a.date_update,'$dateFrom')>=0 and datediff(a.date_update,'$dateTo') <= 0 group by b.id" ;
        break;
        
        case 3:  
        $sql = " select sum(a.num*(a.price +a.area_extprice)) as money_total,u.username as date_sum   from 
                cus_history_product$tbl_postfix a join tbl_user as u on a.user_update = u.id  
                where datediff(a.date_update,'$dateFrom')>=0 and datediff(a.date_update,'$dateTo') <= 0 group by u.id" ;
                break;
        case 4:  
        $sql = " select money_total as money_total,DATE_FORMAT(date_sum,'%d/%m/%Y') date_sum from fm_summary where service_id = $hotel_id 
        and datediff(date_sum,'$dateFrom')>=0 and datediff(date_sum,'$dateTo') <= 0 order by date_sum asc" ;
        break;
    }
    
    //echo $sql;
    $arr =array();
    try{
       $arr = $objDbUpdate->GetArray($sql); 
    }catch(Error $e){
      $arr = $arr;  
    }

     return  $arr  ;
} 


   
?>